<?php
/**
 * Object Relational Persistence Mapping Class for table: template_equipment
 *
 * @package	com.athena
 * @author	LHV
 */
$loader->requireOnce('ordo/EquipmentType.class.php');

class TemplateEquipment extends ORDataObject {

	/**#@+
	 * Fields of table: template_equipment mapped to class members
	 */
	var $template_equipment_id		= '';
	var $name		= '';
	var $description		= '';
	/**#@-*/

	const LIMITED_COMMON_TEMPLATES = 5;
	/**
	 * DB Table
	 */
	var $_table = 'template_equipment';

	/**
	 * Primary Key
	 */
	var $_key = 'template_equipment_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'TemplateEquipment';

	/**
	 * Handle instantiation
	 */
	function TemplateEquipment() {
		parent::ORDataObject();
	}

	function getAllTemplate() {
		$ds = new Datasource_sql();
		$sql = array(
					'cols'	=> 	"$this->_key, name, description",
					'from'	=> 	"$this->_table",
					'limit'	=>  self::LIMITED_COMMON_TEMPLATES
					);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}

	//TODO localization
	function getMedicines($templateId){
		$field	=	L10nHelper::getLocale();
		$nameField	=	L10nHelper::changeField($field, "name");
		$quantityTitleField	=	L10nHelper::changeField($field, "quantity_title");	
		$ds = new Datasource_sql();
		$sql = array(
					'cols'	=> 	"item.surgery_equipment_id medicine_id, item.$quantityTitleField quantity_title, item.quantity,
								et.$nameField type, se.$nameField name",
					'from'	=> 	"$this->_table fte
								INNER JOIN template_equipment_item item ON (fte.template_equipment_id = item.template_equipment_id)
								INNER JOIN surgery_equipment se ON (item.surgery_equipment_id = se.surgery_equipment_id)
								INNER JOIN equipment_type et ON (se.equipment_type_id = et.equipment_type_id)",
					'where'	=>	" fte.template_equipment_id = $templateId AND se.equipment_type_id != ".EquipmentType::EQUIPMENT_ID
					);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
		
	//TODO localization
	function getEquipments($templateId){
		$field	=	L10nHelper::getLocale();
		$nameField	=	L10nHelper::changeField($field, "name");
		$quantityTitleField	=	L10nHelper::changeField($field, "quantity_title");	
		$ds = new Datasource_sql();
		$sql = array(
					'cols'	=> 	"se.surgery_equipment_id equipment_id, et.$nameField type, se.$nameField name,
								item.quantity, item.$quantityTitleField quantity_title",
					'from'	=> 	"$this->_table fte
								INNER JOIN template_equipment_item item ON (fte.template_equipment_id = item.template_equipment_id)
								INNER JOIN surgery_equipment se ON(item.surgery_equipment_id = se.surgery_equipment_id)
								INNER JOIN equipment_type et ON(se.equipment_type_id = et.equipment_type_id)",
					'where'	=>	" item.template_equipment_id = $templateId AND se.equipment_type_id = ".EquipmentType::EQUIPMENT_ID
					);
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}

}
?>
